import Vue from 'vue'
import VueRouter from 'vue-router'
import recommend from '../views/recommend.vue'
import store from '@/store';
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login',
  },
  {
    path:"/login",
    name:"login",
    meta:{
      headerflag:true
    },
    component:()=>import("../components/base/login.vue")
  },
  {
    path:'/recommend',
    name:'recommend',
    component:recommend,
  },
  {
    path:"/recommend/musiclist",
    name:'musiclist',
    meta:{
      headerflag:true
    },
    component:()=>import('../components/recommend/musiclist.vue')
  },
  {
    path:"/userInfo",
    redirect:"/userInfo/like",
    name:"userInfo",
    children:[
      {path:"like",component:()=>import('../components/userinfo/musiclists/like.vue'),meta:{headerflag:true},},
      {path:"establish",component:()=>import('../components/userinfo/musiclists/establish.vue'),meta:{headerflag:true}},
      {path:"collection",component:()=>import('../components/userinfo/musiclists/collection.vue'),meta:{headerflag:true}}
    ],
    meta:{
      headerflag:true
    },
    component:() => import('../components/userinfo/unerinfo.vue'),
      // 独享路由守卫
    beforeEnter: (to, from, next) => {
      // let login = store.state.login || window.localStorage.getItem("login");
      let login = store.state.login;
      console.log(login);
        // 判断登录情况
        if(!login){
          next("/login");
          return;
        }else{
          next();
        }

    }
  },
  {
    path:"/sing",
    name:"sing",
    component:() => import('../views/sing.vue'),
  },
  {
    path:"/sing/singlist",
    meta:{
      headerflag:true
    },
    component:() => import('../components/sing/singlist.vue'),
  },
  {
    path:"/rank",
    name:"rank",
    component:() => import('../views/rank.vue'),
  },
  {
    path:"/rank/musicRank",
    meta:{
      headerflag:true
    },
    component:()=>import('../components/rank/musicRank.vue')
  },
  {
    path:"/search",
    name:"search",
    meta:{
      headerflag:true
    },
    component:()=>import("../views/search.vue")
  },
  {
    path:"/mv",
    name:"mv",
    component:()=>import("../views/mv.vue")
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// router.beforeEach((to,from,next)=>{
//   console.log(to.path);
//   console.log(from.path);
//   console.log(store.state.login);
//   let login = store.state.login  

//   // 登录界面
//   if(to.path == "/login"){
//     next();
//     return
//   }else{
//     // 判断登录情况
//     if(!login){
//       next("/login");
//       return
//     }else{
//       next()
//     }
//   }
// })

export default router
